const { qiniuUpload, makeCrypto, trimSpace,returnInfo } = require('../lib/utils'),
        config = require('../config/config'),
        {adminInfo} = require('../config/config'),
        {addAdmin, login} = require('../service/admin'),
        {LOGIN} = require('../config/err_config');
class Admin {
    // async testQiniu() {
    //     const qiniu = config.qiniu;
    //     try {
    //         const imgData = await qiniuUpload({
    //             url: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605181531778&di=2b7524580da440cdabd494d411e456be&imgtype=0&src=http%3A%2F%2Fmedia-cdn.tripadvisor.com%2Fmedia%2Fphoto-s%2F01%2F3e%2F05%2F40%2Fthe-sandbar-that-links.jpg',
    //             bucket: qiniu.bucket.blogsimg.bucket_name,
    //             ak: qiniu.keys.ak,
    //             sk: qiniu.keys.sk,
    //             ext: '.jpg'
    //         })
           
    //     } catch (error) {
    //         console.log(error)
    //     }
    // }
    async createAdmin(ctx){
        try {
            const { password } = ctx.request.body;
            let data = '';
            if(password){
                data = {
                    username: adminInfo.username,
                    password: makeCrypto(password)
                }
            }else{
                adminInfo.password = makeCrypto(adminInfo.password)
                data = adminInfo
            } 
        
            const result = await addAdmin(data);
            ctx.body = result ? returnInfo(LOGIN.UPDATE_ADMIN_SUCCESS)
                    : returnInfo(LOGIN.UPDATE_ADMIN_ERROR)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(LOGIN.UPDATE_ADMIN_ERROR)
        }
        
    }
    async loginAction(ctx,next){
        try {
            const {username,password} = ctx.request.body;
            if(!username||!password){
                ctx.body = returnInfo(LOGIN.INVALID_OPERATION)
                return
            }
            if(trimSpace(username).length<=0){
                ctx.body = returnInfo(LOGIN.INVALID_USERNAME_LENGTH);
                return
            }
            if(trimSpace(password).length<=0){
                ctx.body = returnInfo(LOGIN.INVALID_PASSWORD_LENGTH)
            }
            const userInfo = {
                username:trimSpace(username),
                password:makeCrypto(trimSpace(password))
            }
            const result = await login(userInfo);
            if(result === 10003){
                ctx.body = returnInfo(LOGIN.USERNAME_NOT_EXIST)
                return
            }
            if(result === 10004){
                ctx.body = returnInfo(LOGIN.PASSWORD_ERROR)
                return
            }
            if(!ctx.session.userInfo){
                ctx.session.userInfo = result;
            }
            return ctx.body = returnInfo(LOGIN.SUCCESS,ctx.session.userInfo)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(LOGIN.ERROR)
        }
       
    }
    async loginCheck(ctx,next){
        if(ctx.session && ctx.session.userInfo){
            ctx.body = returnInfo(LOGIN.LOGIN_STATUS)
            return
        }
        ctx.body = returnInfo(LOGIN.NOT_LOGIN_STATUS)
    }
    async logoutAction(ctx,next){
        try {
            
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(LOGIN.LOGOUT_ERROR)
        }
        delete ctx.session.userInfo;
        ctx.body = returnInfo(LOGIN.LOGOUT_SUCCESS)
    }
}      
module.exports = new Admin();